<?php
namespace backend\controllers;

use common\models\CrawlMerge;
use Yii;
use yii\helpers\Json;
use yii\web\Controller;
use MongoDB;

/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/01/11
 */
class MergeController extends Controller {
    public $enableCsrfValidation = false;

    public function actionBase() {
        return $this->render('base');
    }

    public function actionEditshiprule() {
        return $this->render('editshiprule');
    }

    public function actionEditbaserule() {
        return $this->render('editbaserule');
    }

    public function actionGetfields() {
        $this->asJson([
            'errno' => 0,
            'data' => [
                'innfins' => CrawlMerge::INNFINS_FIELDS,
                'divebooker' => CrawlMerge::DIVEBOOKER_FIELDS,
                'own' => []
            ]
        ]);
    }

    public function actionGettargetfields() {
        $this->asJson([
            'errno' => 0,
            'data' => [
                'target' => CrawlMerge::TARGET_FIELDS,
            ]
        ]);
    }

    public function actionGetvalue() {
        $platform = Yii::$app->request->get('platform');
        $ship_id = Yii::$app->request->get('ship_id');
        $source_field = Yii::$app->request->get('source_field');
        $this->asJson([
            'errno' => 0,
            'data' => ['value' => 'Mama']
        ]);
    }

    /**
     * 前台目标字段列表
     */
    public function actionGetbaserule() {
        $rules = Yii::$app->db->createCommand("SELECT * FROM `merge_fieldmap_base`")->queryAll();
        $this->asJson([
            'errno' => 0,
            'data' => ['rules' => $rules]
        ]);
    }

    public function actionGetshiprule() {
        $ship_id = Yii::$app->request->get('ship_id');
        $base_rules = Yii::$app->db->createCommand("SELECT * FROM `merge_fieldmap_base`")->queryAll();
        $rules = Yii::$app->db->createCommand("SELECT `target_field`,`platform`,`source_field` FROM `merge_fieldmap` WHERE `ship_id`=$ship_id")->queryAll();
        $rules1 = $rules2 = [];
        foreach ($base_rules as $v) {
            $rules1[$v['target_field']] = $v;
        }
        foreach ($rules as $v) {
            $rules2[$v['target_field']] = $v;
        }
        $rules = array_values(array_merge($rules1, $rules2));

        $this->asJson([
            'errno' => 0,
            'data' => ['rules' => $rules]
        ]);
    }

    public function actionBaserule() {
        $rules = Yii::$app->request->post('rules');
        $rules = json_decode($rules, true);
        $values = '';
        foreach ($rules as $k => $v) {
            $values .= "('{$v['target_field']}','{$v['platform']}','{$v['source_field']}'),";
        }
        $values = rtrim($values, ',');
        try {
            Yii::$app->db->createCommand("REPLACE INTO `merge_fieldmap_base` VALUES $values")->execute();
            $this->asJson([
                'errno' => 0,
                'errmsg' => 'success'
            ]);
        } catch (\Exception $e) {
            $this->asJson([
                'errno' => 1,
                'errmsg' => 'fail:' . $e->getMessage()
            ]);
        }
    }

    public function actionShiprule() {
        $ship_id = Yii::$app->request->post('ship_id');
        $rules = Yii::$app->request->post('rules');
        $rules = json_decode($rules, true);
        $values = '';
        foreach ($rules as $k => $v) {
            $values .= "($ship_id,'{$v['target_field']}','{$v['platform']}','{$v['source_field']}'),";
        }
        $values = rtrim($values, ',');
        try {
            Yii::$app->db->createCommand("REPLACE INTO `merge_fieldmap` (`ship_id`,`target_field`,`platform`,`source_field`) VALUES $values")->execute();
            $this->asJson([
                'errno' => 0,
                'errmsg' => 'success'
            ]);
        } catch (\Exception $e) {
            $this->asJson([
                'errno' => 1,
                'errmsg' => 'fail:' . $e->getMessage()
            ]);
        }
    }

    public function actionTest(){
        $mongo = new MongoDB\Client("mongodb://localhost:27017");
        $id = Yii::$app->request->get('id');
        $collection = $mongo->dive->innfins;
        if($id){
            $item = $collection->findOne(['_id' => $id]);
        }else{
            $item = $collection->findOne();
        }
        $this->asJson($item);
    }
    public function actionTest2(){
        $mongo = new MongoDB\Client("mongodb://localhost:27017");
        $id = Yii::$app->request->get('id');
        $collection = $mongo->dive->innfins_v2;
        if($id){
            $item = $collection->findOne(['_id' => $id]);
        }else{
            $item = $collection->findOne();
        }
        $this->asJson($item);
    }
}
